package com.lucenten.collect.config.controller;

import cn.quevo.common.annotation.RequiresPermissions;
import cn.quevo.common.entity.ResultEntity;
import cn.quevo.common.enums.ResultStatusEnum;
import cn.quevo.common.lang.StringUtils;
import cn.quevo.core.spring.SpringController;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import com.lucenten.collect.config.entity.CollectConf;
import com.lucenten.collect.config.entity.query.CollectConfQuery;
import com.lucenten.collect.config.service.CollectConfService;

import java.util.Map;

/**
 * 采集设置 RestController
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2019/04/27 11:04:02
 * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
 */
@Api(value="采集设置", tags = "采集设置")
@RestController("collectConfController")
@RequestMapping("/collect/")
public class CollectConfRestController extends SpringController{

    @Resource(name="collectConfService")
    private CollectConfService collectConfService;

    /**
     * 分页加载采集设置
     *
     * @param collectConfQuery
     * @return
     * @author July july_sky@foxmail.com
     * @date 2019/04/27 11:04:02
     */
    @ApiOperation(value = "分页加载采集设置", notes = "")
    @GetMapping(value="collectConf")
    @RequiresPermissions("collect:collectConf:list")
    public ResultEntity page(CollectConfQuery collectConfQuery){
        return collectConfService.getPage(collectConfQuery);
    }

    /**
     * 保存采集设置
     *
     * @param collectConf
     * @return
     * @author July july_sky@foxmail.com
     * @date 2019/04/27 11:04:02
     */
    @ApiOperation(value = "保存采集设置", notes = "")
    @PostMapping(value="collectConf")
    @RequiresPermissions("collect:collectConf:create")
    public ResultEntity create(@RequestBody CollectConf collectConf){
        if(collectConfService.create(collectConf) <= 0){
            return new ResultEntity(ResultStatusEnum.RECORD_REPEAT);
        }
        return ResultEntity.success();
    }

    /**
     * 修改采集设置
     *
     * @param collectConf
     * @param id
     * @return
     * @author July july_sky@foxmail.com
     * @date 2019/04/27 11:04:02
     */
    @ApiOperation(value = "修改采集设置")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "设置ID", required = true, dataType = "Long", paramType = "path")
    })
    @PutMapping(value="collectConf/{id:\\d+}")
    @RequiresPermissions("collect:collectConf:update")
    public ResultEntity update(@RequestBody CollectConf collectConf, @PathVariable("id") Long id){
        if (StringUtils.isBlank(id)) {
            return ResultEntity.fail(ResultStatusEnum.RECORD_UNSELECTED);
        }
        collectConf.setId(id);
        if (StringUtils.isBlank(this.collectConfService.update(collectConf))) {
            return ResultEntity.fail(ResultStatusEnum.RECORD_REPEAT);
        }
        return ResultEntity.success();
    }
}
